﻿/* Created by Subodh Shrestha
 * Contact: forsubodh@gmail.com 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TravelExpertsManagementConsole
{
    public partial class formProduct : Form
    {
        public enum methodcalltype
        {
            add = 0,
            update = 1,
        }

        public formProduct(frmContainer parent)
        {
            InitializeComponent();

            MdiParent = parent;
            setAccessControls(parent.accessLevel);
        }

        public void setAccessControls(int accessLevel)
        {
            switch (accessLevel)
            {
                case 3:
                    btnNew.Enabled = false;
                    btnClear.Enabled = false;
                    btnSubmit.Enabled = false;
                    break;
                default:
                    break;
            }
        }

        private void formProduct_FormClosing(object sender, FormClosingEventArgs e)//Do not know where to connect this one
        {
            frmContainer parent = (frmContainer)MdiParent;

            parent.childProduct = null;
        }

        private void formProduct_Load(object sender, EventArgs e)
        {
            refreshList();
        }

        private void refreshList()
        {
            try
            {
                DataAccess<Product> myDataAccess = new DataAccess<Product>();
                List<Product> productList= myDataAccess.select();
                listBoxProduct.DataSource = productList;
                
                tbProdId.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e) //Button to add a new Product
        {
            if (!validateInput(methodcalltype.add))
            {
                return;
            }
            try
            {
                DataAccess<Product> myDataAccess = new DataAccess<Product>();
                Product newProduct = new Product();
                newProduct.ProductName = tbProdName.Text;
                newProduct.SupplierList = new List<Supplier>();
                foreach (Supplier supp in listBoxProdSuppliers.Items)
                {
                    newProduct.SupplierList.Add(supp);
                }
                myDataAccess.Add(newProduct);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            refreshList();
        }

        private bool validateInput(methodcalltype func)
        {
            string errMsg = "";
            if (func == methodcalltype.add)
            {
                if (tbProdId.Text != "")
                {
                    errMsg += "Cannot create duplicate product" + Environment.NewLine;
                }
            }
            if (tbProdName.Text == "")
            {
                errMsg += "Please enter Product Name" + Environment.NewLine;
            }
            else
            {
                Double number;
                if (Double.TryParse(tbProdName.Text, out number))
                {
                    errMsg += "Product Name must be string" + Environment.NewLine;
                }
            }
            if (errMsg == "")
            {
                return true;
            }
            else
            {
                MessageBox.Show(errMsg);
                return false;
            }
        }

        private void btnClear_Click(object sender, EventArgs e) //Button to Delete a product from the form 
        {
            if (listBoxProduct.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a Product");
                return;
            }
            try
            {
                DataAccess<Product> myDataAccess = new DataAccess<Product>();
                Product selectedProduct = (Product)listBoxProduct.SelectedItem;
                myDataAccess.Delete(selectedProduct);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            refreshList();
        }

        private void btnUpdate_Click_1(object sender, EventArgs e) //Button to Update the form 
        {
            if (listBoxProduct.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a Product");
                return;
            }
            if (!validateInput(methodcalltype.update))
            {
                return;
            }
            try
            {
                DataAccess<Product> myDataAccess = new DataAccess<Product>();
                Product updateProduct = new Product();
                updateProduct.ProductID = int.Parse(tbProdId.Text);
                updateProduct.ProductName = tbProdName.Text;
                updateProduct.SupplierList = new List<Supplier>();
                foreach (Supplier supp in listBoxProdSuppliers.Items)
                {
                    updateProduct.SupplierList.Add(supp);
                }
                myDataAccess.Update(updateProduct);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            refreshList();
        }

        private void btnNew_Click(object sender, EventArgs e) //Button to clear the form 
        {
            tbProdId.Text = "";
            tbProdName.Text = "";
            listBoxProduct.SelectedIndex = -1;
            listBoxProdSuppliers.DataSource = null;
        }

        private void listBoxProduct_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxProduct.SelectedIndex == -1)
            {
                return;
            }
            Product selectedProduct = (Product)listBoxProduct.SelectedItem;
            tbProdId.Text = selectedProduct.ProductID.ToString();
            tbProdName.Text = selectedProduct.ProductName;
            listBoxProdSuppliers.DataSource = selectedProduct.SupplierList;
        }

        private void addToProdSuppliers_Click(object sender, EventArgs e)
        {
            if (listBoxAllSuppliers.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a Supplier");
                return;
            }

            Supplier selectedSupplier = (Supplier)listBoxAllSuppliers.SelectedItem;
            List<Supplier> supplierList;
            if ((List<Supplier>)listBoxProdSuppliers.DataSource != null)
            {
                supplierList = (List<Supplier>)listBoxProdSuppliers.DataSource;
            }
            else
            {
                supplierList = new List<Supplier>();
                listBoxProdSuppliers.DataSource = supplierList;
            }
            //check if the supplier-to-add already exists in the product's supplier list
            Supplier findMatch = supplierList.Find(delegate(Supplier s) { return s.SupplierId == selectedSupplier.SupplierId; });
            if (findMatch == null)
            {
                supplierList.Add(selectedSupplier);
            }
            ((CurrencyManager)listBoxProdSuppliers.BindingContext[listBoxProdSuppliers.DataSource]).Refresh();
        }

        private void removeFromProdSuppliers_Click(object sender, EventArgs e)
        {
            if (listBoxProdSuppliers.DataSource == null)
            {
                return;
            }
            if (listBoxProdSuppliers.SelectedIndex == -1)
            {
                MessageBox.Show("Please select a Supplier");
                return;
            }

            Supplier selectedSupplier = (Supplier)listBoxProdSuppliers.SelectedItem;
            List<Supplier> supplierList = (List<Supplier>)listBoxProdSuppliers.DataSource;
            supplierList.Remove(selectedSupplier);
            ((CurrencyManager)listBoxProdSuppliers.BindingContext[listBoxProdSuppliers.DataSource]).Refresh();
        }

        private void formProduct_Enter(object sender, EventArgs e)
        {
            listBoxAllSuppliers.DataSource = DataAccess<Product>.setAllSuppliers();
            refreshList();
        }

    }
}
